package com.powernode.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.powernode.common.Constants;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


//验证码
@RestController
@RequestMapping("captcha")
public class CodeController {
    /** 
     * 生成一个验证码
     * @param session
     * @param response
     */
    @RequestMapping("captcha.action")
    public void captcha(HttpSession session,HttpServletResponse response){
        //定于图形的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 76, 1, 200);
        //得到正确的验证码
        String code = lineCaptcha.getCode();
        //把正确的验证码放入session
        session.setAttribute(Constants.CAPTCHA_CODE_KEY,code);
        try {
            lineCaptcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
